package com.javastudy.basis;

import com.javastudy.data.Person;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 测试对象引用传递
 */
public class ObjectTest {

    public static final List<String> books = new ArrayList<>();
    public static final int PI=3;
    public static int AGE=100;
    public final int HEIGH = 2;

    public static void main(String[] args){
        //PI = 4; //这里不可以修改，编译报错
        AGE = 40;
        //HEIGH = 3;直接报错
        //System.out.println(HEIGH);
        books.add("book-1");
        Person person = new Person();
        person.setName("originalName");
        changeObject(person,books);
        System.out.println("name="+person.getName());// ChangedName
        System.out.println("books="+books); //[book-1, Book-2]

        List<String> anotherBooks = new ArrayList<>();
        anotherBooks.add("Sea");
        //books = anotherBooks; //报错：cannot assign a value to final variable

        person.setName("originalName");
        keepObject(person);
        System.out.println("name="+person.getName());// originalName
    }

    private static void changeObject(Person person,List<String> books){
        person.setName("ChangedName");
        books.add("Book-2");
    }

    private static void keepObject(Person person){
        Person newPerson = new Person();
        newPerson.setName("NewName");
        person = newPerson;
    }

}
